//
//  LocationList.m
//  MapEdit
//
//  Created by Steven Canfield on 7/14/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "LocationList.h"


@implementation LocationList
- (id)initWithData:(NSData *)locationData {
	self = [super init];
	if( self ) {
		_locations = malloc( [locationData length] );
		[locationData getBytes:_locations length:[locationData length]];
		_locationCount = [locationData length] / sizeof( chk_mrgn_t );
		
		int idx;
		for( idx = 0; idx < _locationCount; idx++ ) {
			swap_chk_mrgn( &(_locations[idx]) );
		}
		
	}
	return self;
}

- (void)dealloc {
	free( _locations );
	[_stringManager release];
	[super dealloc];
}

- (void)setStringManager:(StringManager *)manager {
	if( manager != _stringManager ) {
		[_stringManager release];
		_stringManager = [manager retain];
	}
	
	int index = 0;
	for( index = 0; index < _locationCount; index++ ) {
		//NSLog( @"%@ = %@", [self nameForLocationAtIndex: index ], NSStringFromRect( [self rectForLocationAtIndex: index ] ));
	}

}

- (int)locationCount {
	return _locationCount;
}

- (NSRect)rectForLocationAtIndex:(int)index {
	return NSMakeRect( _locations[ index ].start_x, _locations[ index ].start_y, _locations[ index ].end_x - _locations[ index ].start_x, _locations[ index ].end_y - _locations[ index ].start_y);
}

- (NSString *)nameForLocationAtIndex:(int)index {
	return [NSString stringWithFormat:@"%i = %@", index, [_stringManager stringAtIndex:_locations[ index ].name_index]];
}
@end
